package com.example.yls.demoa;

import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private Button btnInsertStu;
    private Button btnQueryStu;
    private Button btnDelStu;
    private Button btnUpdateStu;
    private Button btnInsertSco;
    private Button btnQuerySco;

    private TextView txtResult;

    private Handler handler;
    private MyContentObserver observer;
    public static int MSG_STUDENT_CHANGED;

    private MyReceiver receiver;
    private IntentFilter intentFilter;

    private EditText edtNo;
    private EditText edtName;
    private EditText edtAge;
    private Button btnAdd;
    private View btnQuery;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        receiver =new MyReceiver();
        IntentFilter intentFilter =new IntentFilter();
        intentFilter.addAction("Broadcast_Stu_Changed");
        registerReceiver(receiver,intentFilter);

        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                Log.e("Handler","handleMessage"+message.what);
                if(message.what==MSG_STUDENT_CHANGED){
                    queryContent();
                    return true;
                }
                return false;
            }

        });

        Uri uri=Uri.parse("content://com.gdcp.student/student");
        observer=new MyContentObserver(handler);
        getContentResolver().registerContentObserver(uri,true,observer);

        btnInsertStu = findViewById(R.id.btn_insert_stu);
        btnInsertStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                ContentValues values = new ContentValues();
                values.put("stuno", "171001");
                values.put("name", "zhangsan");
                values.put("age", 20);
                getContentResolver().insert(uri, values);

                getContentResolver().notifyChange(uri,observer);
            }
        });

        txtResult = findViewById(R.id.txt_result);
        btnQueryStu = findViewById(R.id.btn_query_stu);
        btnQueryStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
               Cursor cursor = getContentResolver().query(uri, null, null, null, null);
               if(cursor != null && cursor.getCount() > 0){
                   StringBuffer buffer = new StringBuffer();
                   for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
                       String stuno = cursor.getString(0);
                       String name = cursor.getString(1);
                       int age = cursor.getInt(2);
                       buffer.append(stuno).append("  ").append(name).append("  ").append(age).append("\n");
                   }

                   txtResult.setText(buffer);
               }else{
                   txtResult.setText("行数为0");
               }
            }
        });

        btnDelStu = findViewById(R.id.btn_del_stu);
        btnDelStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                String where ="name = 'zhangsan'";
                int rows = getContentResolver().delete(uri, where, null);
                Toast.makeText(MainActivity.this, "删除了"+ rows + "行", Toast.LENGTH_SHORT).show();
            }
        });

        btnUpdateStu = findViewById(R.id.btn_update_stu);
        btnUpdateStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                ContentValues contentValues = new ContentValues();
                contentValues.put("age", 30);
                String where = "age = 20";
                int rows = getContentResolver().update(uri, contentValues, where, null);
                Toast.makeText(MainActivity.this, "更新了"+ rows + "行", Toast.LENGTH_SHORT).show();
            }
        });

        btnInsertSco = findViewById(R.id.btn_insert_sco);
        btnInsertSco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/score");
                ContentValues values = new ContentValues();
                values.put("stuno", "171001");
                values.put("english", 80);
                values.put("android", 85);
                getContentResolver().insert(uri, values);
            }
        });

        btnQuerySco = findViewById(R.id.btn_query_sco);
        
        btnQuerySco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/score");
                Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                if(cursor != null && cursor.getCount() > 0){
                    StringBuffer buffer = new StringBuffer();
                    for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
                        String stuno = cursor.getString(0);
                        int english  = cursor.getInt(1);
                        int android = cursor.getInt(2);
                        buffer.append(stuno).append("  ").append(english).append("  ").append(android).append("\n");
                    }

                    txtResult.setText(buffer);
                }
            }
        });

        btnQuery = findViewById(R.id.btn_query);
        btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                if (cursor != null && cursor.getCount() > 0) {
                    StringBuffer buffer = new StringBuffer();
                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        String stuno = cursor.getString(0);
                        String name = cursor.getString(1);
                        int age = cursor.getInt(2);
                        buffer.append(stuno).append("  ").append(name).append("  ").append(age).append("\n");
                    }

                    txtResult.setText(buffer);
                } else {
                    txtResult.setText("行数为0");
                }
            }
        });


        edtNo = findViewById(R.id.edtNo);
        edtName = findViewById(R.id.edtName);
        edtAge = findViewById(R.id.edtAge);
        btnAdd = findViewById(R.id.btn_add);

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String no = edtNo.getText().toString();
                String name = edtName.getText().toString();
                int age = Integer.parseInt(edtAge.getText().toString());
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                ContentValues values = new ContentValues();
                values.put("stuno", no);
                values.put("name", name);
                values.put("age", age);
                getContentResolver().insert(uri, values);
            }
        });

    }
    class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e("MyReceiver","onReceive");
            Uri uri = Uri.parse("content://com.gdcp.student/student");
            Cursor cursor = getContentResolver().query(uri, null, null, null, null);
            if(cursor != null && cursor.getCount() > 0){
                StringBuffer buffer = new StringBuffer();
                for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
                    String stuno = cursor.getString(0);
                    String name = cursor.getString(1);
                    int age = cursor.getInt(2);
                    buffer.append(stuno).append("  ").append(name).append("  ").append(age).append("\n");
                }

                txtResult.setText(buffer);
            }else{
                txtResult.setText("行数为0");
            }
        }
    }
    private void queryContent(){
    }
}
